go to previous page   go to home page   go to next page hear noise

Answer:

No. Running a String object's method does not change that object, but creates a new String object.


Objects Creating Objects

It seems a little odd that an object can create another object. This is not how real-world objects usually behave. Bricks do not create other bricks. Cars do not create other cars. But software objects often create new software objects.

Software objects are created:

  1. By using a constructor.
  2. By calling a method that constructs an object.

There is no difference between objects created one way or the other.

The substring() method of a String object creates a new object by asking the Java virtual computer to create a new object. The virtual computer creates the new object in the usual way by following the instructions contained in the class.


QUESTION 14:

Do you imagine that there are other String methods that create new String objects?